package com.doform.android.xianggucheng.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.CategoryQueryBean;
import com.doform.android.xianggucheng.holder.PopMultiHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-07-19.
 * GitHub：https://github.com/cxydxpx
 */

public class CommonCategoryAdapter extends RecyclerView.Adapter<PopMultiHolder> {

    private Context mContext;
    private List<CategoryQueryBean> mDatas;


    public CommonCategoryAdapter(Context context, List<CategoryQueryBean> datas) {
        this.mContext = context;
        List<CategoryQueryBean> data = new ArrayList<>();

        for (int i = 0; i < datas.size(); i++) {
            CategoryQueryBean bean = datas.get(i);
            if (bean.getFid().equals("0")) {
                mDatas.add(bean);
            }
        }
        this.mDatas = data;
    }

    @Override
    public PopMultiHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.pop_selete_category_item, null);
        return new PopMultiHolder(view);
    }

    @Override
    public void onBindViewHolder(PopMultiHolder holder, final int position) {

        final CategoryQueryBean bean = mDatas.get(position);

        final CheckBox checkBox = holder.checkBox;

        TextView tv_name = holder.tv_name;

        tv_name.setText(bean.getName());

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkBox.setChecked(false);
                    bean.setSelete(false);
                } else {
                    checkBox.setChecked(true);
                    bean.setSelete(true);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void notifyDataSetChanged(List<CategoryQueryBean> data) {
        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();
    }


    protected OnItemDelete mOnItemClick;

    public interface OnItemDelete {
        void onItemClick(String id, String name);
    }

    public void setOnItemClickListener(OnItemDelete onItemClickListener) {
        this.mOnItemClick = onItemClickListener;
    }

}
